package traditionalthread;

public class XunLeiInterview2 {

	public static void main(String[] args){
		
		printChar(5, 5);

	}
	
	public static void printChar(final int n,final int m){
		for( int i=1;i<=n;i++){
			final int j =i;
			new Thread(){
				public void run(){
					new ThreadDemo2().demoRun(j, m, n);
				}
			}.start();
		}
	}
}
class ThreadDemo2{
	private static char allTag = 'A';
	private static long begin;
	private static long end;
	public synchronized int demoRun(int i,int m,int n){
		char tag = (char) (allTag+(i-1));
		int times =1;
		while(times<=m){
			if(tag!=allTag){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				if(tag=='A'&&times==1){
					begin=System.currentTimeMillis();
				}
				System.out.println(tag);
				if(allTag==('A'+(n-1))){
					allTag='A';
				}else{
					allTag+=1;
				}
				times++;
			}
		}
		if(tag==('A'+(n-1))&&times==m){
			end=System.currentTimeMillis();
			System.out.println(end-begin);
		}
		return 0;
	}
}